package org.mimizet;

import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.converters.reflection.FieldDictionary;
import com.thoughtworks.xstream.converters.reflection.ImmutableFieldKeySorter;
import com.thoughtworks.xstream.converters.reflection.Sun14ReflectionProvider;
import com.thoughtworks.xstream.io.xml.DomDriver;

import java.io.InputStream;
import java.io.OutputStream;

/**
 * User: Admin
 * Date: 29.08.11
 * Time: 0:17
 */
public class XStreamSerializer implements Serializer {
    private final XStream xstream = new XStream(new Sun14ReflectionProvider(
                    new FieldDictionary(new ImmutableFieldKeySorter())),
                    new DomDriver("utf-8"));

    public void serialize(Object object, OutputStream out) {
        xstream.toXML(object, out);
    }

    public <T> T deserialize(InputStream is, Class<? extends T> aClass) {
        return (T) deserialize(is);
    }

    public Object deserialize(InputStream is) {
        return xstream.fromXML(is);
    }
}
